package com.xgimi.locales.ui

import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.ui.DialogWrapper
import com.xgimi.locales.bean.ExportConfig
import com.xgimi.locales.bean.IConfig
import com.xgimi.locales.bean.InputConfig
import com.xgimi.locales.ext.labelSize
import com.xgimi.locales.utils.FileUtil
import okio.ByteString.Companion.encode
import java.io.File
import javax.swing.*

/**
 * 选择界面
 */
class SelectModelDialog (private val anActionEvent: AnActionEvent) : DialogWrapper(true) {

    companion object {
        @JvmStatic
        fun show(anActionEvent: AnActionEvent) {
            SelectModelDialog(anActionEvent).apply {
                pack()
                showAndGet()
            }
        }
    }

    init {
        init()
        title = "选择模式"
        setSize(300, 300)
    }

    override fun createCenterPanel(): JComponent = Box.createVerticalBox().apply {
        add(createModelTypeBox())
    }

//    private val exportRB: JRadioButton by lazy {
//        val exportRB = JRadioButton("导出")
//        exportRB.isSelected = true
//        exportRB
//    }
//    private val importRB: JRadioButton by lazy { JRadioButton("导入") }
    private var exportRB: JRadioButton? = null
    private var importRB: JRadioButton? = null

    private fun createModelTypeBox(): JComponent = Box.createHorizontalBox().also { box ->
        box.add(JLabel("选择模式:").labelSize())
        box.add(Box.createHorizontalGlue())
//        ButtonGroup().also { group ->
//            group.add(exportRB)
//            group.add(importRB)
//        }
        ButtonGroup().also { group ->
            exportRB = JRadioButton("xml导出excel")
            exportRB?.isSelected = true
            group.add(exportRB)
            importRB = JRadioButton("excel导入xml")
            group.add(importRB)
        }
        //
        box.add(exportRB)
        box.add(Box.createVerticalStrut(20))
        box.add(importRB)
    }

    private val inputConfig by lazy { InputConfig() }
    private val exportConfig by lazy { ExportConfig() }

    override fun doOKAction() {
        super.doOKAction()
        if (exportRB?.isSelected == true) { // 导出
            scanProject(exportConfig, { file->
                //                println("scanProject file:${file.name},path:${file.absolutePath}")
            }) {
                ExportConfigDialog.show(anActionEvent, exportConfig)
            }
        } else { // 导入
            scanProject(inputConfig, { file->
//                println("scanProject file:${file.name},path:${file.absolutePath}")
            }) {
                InputConfigDialog.show(anActionEvent, inputConfig)
            }
        }
    }

    private fun scanProject(
        config: IConfig,
        onProgress: (file:File)->Unit,
        onComplete: () -> Unit
    ) {
        config.projectName = anActionEvent.project?.name ?: "" // 比如 Settings
        config.projectPath = anActionEvent.project?.basePath ?: "" // 比如 D:/work/code/Settings
        //
        println("scanProject 开始扫描项目文件 projectName:${config.projectName} projectPath:${config.projectPath}")
        FileUtil.findAllModule(File(config.projectPath)) {file->
            onProgress(file)
        }.also {
            println("scan dir over module size:${it.size}")
            config.allModuleBeans = it
            FileUtil.findLangByModule(it) { file ->
//                println("findLangByModule ${file.absolutePath}")
            }
            SwingUtilities.invokeLater { onComplete() }
        }
    }

    // anActionEvent.project?.name ?: ""  -> demo
    // anActionEvent.project?.basePath ?: "" -> d:/code/translator
}